﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PmSadis.WebFormsUi.ExpandedControls;
using PmSadis.Repositories.Abstract;
using DevExpress.Web.Data;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxGridLookup;

namespace PmSadis.WebFormsUi.Operator
{
    public partial class Drivers : System.Web.UI.Page
    {
        private IListRepository<Models.Driver> _repo = new Repositories.DriverRepository();
        private IListRepository<Models.Unit> _repoUnits = new Repositories.UnitRepository();
        
        protected void gvDrivers_Init(object sender, EventArgs e)
        {
            var grid = sender as ASPxGridView;
            grid.ForceDataRowType(typeof(Models.Driver));
            grid.SetDefaultSettings();
        }

        public IQueryable<Models.Driver> AllDrivers()
        {
            return _repo.All.OrderBy(x => x.Name);
        }
        public void InsertDriver(string name, string code, decimal costBase, int? unitId)
        {
            var driver = new Models.Driver {
                DriverId = 0,
                Name=name,
                Code=code,
                CostBase=costBase,
                UnitId=unitId
            };
            _repo.Edit(driver);
        }
        public void UpdateDriver(int driverId, DateTime dateCreate, string name, string code, decimal costBase, int? unitId)
        {
            var driver = new Models.Driver
            {
                DriverId = driverId,
                DateCreate=dateCreate,
                Name = name,
                Code = code,
                CostBase = costBase,
                UnitId = unitId
            };
            _repo.Edit(driver);
        }
        public void DeleteDriver(int driverId)
        {
            _repo.Delete(driverId);
        }

        protected void lucUnits_Init(object sender, EventArgs e)
        {
            var lookup = sender as ASPxGridLookup;
            var container = lookup.NamingContainer as GridViewDataItemTemplateContainer;
            if (!container.Grid.IsNewRowEditing)
            {
                var materialtId = (int)container.Grid.GetRowValues(container.VisibleIndex, container.Column.FieldName);
                lookup.GridView.Selection.SelectRowByKey(materialtId);
            }
        }

        protected void gvDrivers_RowInserting(object sender, ASPxDataInsertingEventArgs e)
        {
            var grid = sender as ASPxGridView;

            var index = grid.EditingRowVisibleIndex;
            var lookup = grid.FindEditRowCellTemplateControl(grid.Columns["UnitId"] as GridViewDataColumn, "lucUnits") as ASPxGridLookup;
            var unitId = lookup.Value;
            e.NewValues["UnitId"] = Convert.ToInt32(unitId);
        }
        protected void gvDrivers_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
        {
            var grid = sender as ASPxGridView;
            var dateCreate = grid.GetRowValues(grid.EditingRowVisibleIndex, "DateCreate");
            e.NewValues["DateCreate"] = dateCreate;

            var index = grid.EditingRowVisibleIndex;
            var lookup = grid.FindEditRowCellTemplateControl(grid.Columns["UnitId"] as GridViewDataColumn, "lucUnits") as ASPxGridLookup;
            var unitId = lookup.Value;
            e.NewValues["UnitId"] = Convert.ToInt32(unitId);
        }

        public IQueryable<Models.Unit> Units()
        {
            return _repoUnits.All.OrderBy(x => x.Name);
        }
    }
}